﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diplom.NumericArrays;
using Diplom.Graphics.D3D;
using Diplom.Graphics.D2D;
using System.Threading;
using System.IO;
using Diplom.Wavelets;

namespace Diplom.Forms
{
    public partial class ImageSaver : Form
    {
        Numeric1DArray array1D = null;
        Numeric2DArray array2D = null;

        Numeric1DArray fourierArray = null;
        SaveType saveType;

        public enum SaveType
        {
            Fourier,
            Wavelet
        }

        public ImageSaver(ref Numeric1DArray array1D, ref Numeric2DArray array2D, ref Numeric1DArray fourierArray, SaveType saveType)
        {
            InitializeComponent();

            if (saveType == SaveType.Wavelet)
            {
                this.array1D = array1D;
                this.array2D = array2D;
                this.fourierArray = null;
            }
            else
            {
                this.array1D = array1D;
                this.array2D = null;
                this.fourierArray = fourierArray;

                spektrogramma.Enabled = false;
                skalogramma.Enabled = false;
                skeylogramma.Enabled = false;
                skeleton.Enabled = false;
                black_whiteImage.Enabled = false;
                groupBox4.Enabled = false;
                waveSetting.Enabled = false;
            }
            this.saveType = saveType;
        }

        private void refresh_Click(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void CustomPaint()
        {
            int w = (int)width.Value;
            int h = (int)height.Value;

            resultImage.Image = new Bitmap(w, h);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(resultImage.Image);
            graphics.Clear(Color.White);
            DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

            D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);
            if (axis.Checked)
            {
                D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
            }

            if (saveType == SaveType.Wavelet)
            {
                if (isolineCheckBox.Checked)
                {
                    IsolineVisualizer.DrawIsoline(ref graphics,
                        new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)),
                        ref array2D, (int)isolineCountNumericUpDown.Value);

                    if (axis.Checked)
                    {
                        IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                    }
                }
                else
                {
                    ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), ref array2D, (int)isolineCountNumericUpDown.Value);

                    if (axis.Checked)
                    {
                        IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                    }
                }

                if (black_whiteImage.Checked)
                {
                    graphics.Dispose();
                    Bitmap bmp = new Bitmap(resultImage.Image);
                    for (int i = 40; i < w - 10; i++)
                    {
                        for (int j = 25 + h / 3; j < h * 2 / 3 - 25 + h / 3; j++)
                        {
                            Color rgb = bmp.GetPixel(i, j);
                            int r = (int)(rgb.R * 0.3);
                            int g = (int)(rgb.G * 0.59);
                            int b = (int)(rgb.B * 0.11);
                            rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                            bmp.SetPixel(i, j, rgb);
                        }
                    }
                    resultImage.Image = (Image)bmp;
                    graphics = System.Drawing.Graphics.FromImage(resultImage.Image);
                }
            }
            else
            {
                D2DVisualizer.DrawGraphic(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle);
                if (axis.Checked)
                {
                    D2DVisualizer.DrawSignature(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                }
            }
        }

        private void ImageSaver_ResizeEnd(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void waitForm()
        {
            Wait w = new Wait();
            w.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Введите имя файла для сохранения", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\"))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\");
            }

            if (saveType == SaveType.Wavelet)
            {
                Thread thread = new Thread(new ThreadStart(waitForm));
                thread.Start();


                #region Spektrogramma
                if (spektrogramma.Checked)
                {
                    int w = (int)width.Value;
                    int h = (int)height.Value;

                    Image img = new Bitmap(w, h);
                    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
                    graphics.Clear(Color.White);
                    DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);

                    if (axis.Checked)
                    {
                        D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                    }

                    if (isolineCheckBox.Checked)
                    {
                        IsolineVisualizer.DrawIsoline(ref graphics,
                            new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)),
                            ref array2D, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }
                    else
                    {
                        ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), ref array2D, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }

                    if (black_whiteImage.Checked)
                    {
                        graphics.Dispose();
                        Bitmap bmp = new Bitmap(img);
                        for (int i = 40; i < w - 10; i++)
                        {
                            for (int j = 25 + h / 3; j < h * 2 / 3 - 25 + h / 3; j++)
                            {
                                Color rgb = bmp.GetPixel(i, j);
                                int r = (int)(rgb.R * 0.3);
                                int g = (int)(rgb.G * 0.59);
                                int b = (int)(rgb.B * 0.11);
                                rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                                bmp.SetPixel(i, j, rgb);
                            }
                        }
                        img = (Image)bmp;
                    }
                    img.Save(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\" + textBox1.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                #endregion

                #region Skalogramma
                if (skalogramma.Checked)
                {
                    int w = (int)width.Value;
                    int h = (int)height.Value;

                    Image img = new Bitmap(w, h);
                    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
                    graphics.Clear(Color.White);
                    DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);

                    if (axis.Checked)
                    {
                        D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                    }

                    Numeric2DArray skalogrammaArray = new Numeric2DArray(0);
                    skalogrammaArray = Analitics.Scalogramma(ref array2D);

                    if (isolineCheckBox.Checked)
                    {
                        IsolineVisualizer.DrawIsoline(ref graphics,
                            new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)),
                            ref skalogrammaArray, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref skalogrammaArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }
                    else
                    {
                        ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), ref skalogrammaArray, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref skalogrammaArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }

                    if (black_whiteImage.Checked)
                    {
                        graphics.Dispose();
                        Bitmap bmp = new Bitmap(img);
                        for (int i = 40; i < w - 10; i++)
                        {
                            for (int j = 25 + h / 3; j < h * 2 / 3 - 25 + h / 3; j++)
                            {
                                Color rgb = bmp.GetPixel(i, j);
                                int r = (int)(rgb.R * 0.3);
                                int g = (int)(rgb.G * 0.59);
                                int b = (int)(rgb.B * 0.11);
                                rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                                bmp.SetPixel(i, j, rgb);
                            }
                        }
                        img = (Image)bmp;
                    }
                    img.Save(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\" + textBox1.Text + "_skalogramma" + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                #endregion

                #region Skeylogramma
                if (skeylogramma.Checked)
                {
                    int w = (int)width.Value;
                    int h = (int)height.Value;

                    Image img = new Bitmap(w, h);
                    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
                    graphics.Clear(Color.White);
                    DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);

                    if (axis.Checked)
                    {
                        D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                    }

                    Numeric1DArray skeylogrammaArray = new Numeric1DArray(0);
                    skeylogrammaArray = Analitics.Sceylogramma(ref array2D, true);

                    D2DVisualizer.DrawGraphic(ref graphics, ref skeylogrammaArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle);
                    if (axis.Checked)
                    {
                        D2DVisualizer.DrawSignature(ref graphics, ref skeylogrammaArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                    }

                    img.Save(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\" + textBox1.Text + "_skeylogramma" + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                #endregion

                #region Skeleton
                if (skeleton.Checked)
                {
                    int w = (int)width.Value;
                    int h = (int)height.Value;

                    Image img = new Bitmap(w, h);
                    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
                    graphics.Clear(Color.White);
                    DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);

                    if (axis.Checked)
                    {
                        D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                    }

                    Numeric2DArray skalogrammaArray = new Numeric2DArray(0);
                    skalogrammaArray = Analitics.Scalogramma(ref array2D);
                    Numeric2DArray skeletonArray = new Numeric2DArray(0);
                    skeletonArray = Analitics.Sceleton(ref skalogrammaArray);

                    if (isolineCheckBox.Checked)
                    {
                        IsolineVisualizer.DrawIsoline(ref graphics,
                            new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)),
                            ref skeletonArray, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref skeletonArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }
                    else
                    {
                        ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), ref skeletonArray, (int)isolineCountNumericUpDown.Value);

                        if (axis.Checked)
                        {
                            IsolineVisualizer.DrawSignature(ref graphics, ref skeletonArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                        }
                    }

                    if (black_whiteImage.Checked)
                    {
                        graphics.Dispose();
                        Bitmap bmp = new Bitmap(img);
                        for (int i = 40; i < w - 10; i++)
                        {
                            for (int j = 25 + h / 3; j < h * 2 / 3 - 25 + h / 3; j++)
                            {
                                Color rgb = bmp.GetPixel(i, j);
                                int r = (int)(rgb.R * 0.3);
                                int g = (int)(rgb.G * 0.59);
                                int b = (int)(rgb.B * 0.11);
                                rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                                bmp.SetPixel(i, j, rgb);
                            }
                        }
                        img = (Image)bmp;
                    }
                    img.Save(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\" + textBox1.Text + "_skeleton" + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                #endregion

                thread.Abort();
                thread = null;
            }
            else
            {
                int w = (int)width.Value;
                int h = (int)height.Value;

                Image img = new Bitmap(w, h);
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
                graphics.Clear(Color.White);
                DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle);

                if (axis.Checked)
                {
                    D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(w - 50, h / 3 - 60)), drawStyle, new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                }


                D2DVisualizer.DrawGraphic(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle);
                if (axis.Checked)
                {
                    D2DVisualizer.DrawSignature(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + h / 3), new SizeF(w - 50, h * 2 / 3 - 50)), drawStyle, new Size((int)numericUpDown3.Value, (int)numericUpDown4.Value));
                }

                img.Save(Path.GetDirectoryName(Application.ExecutablePath) + "\\Screen\\" + textBox1.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            MessageBox.Show("Данные сохранены", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ImageSaver_Load(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.CustomPaint();
        }
    }
}
